package tas.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import tas.dao.DaoTerminal;
import tas.domain.Terminal;
import tas.sql.ConectaDb;

/**
 *
 * @author rtarazona
 */
public class DaoTerminalImpl implements DaoTerminal {

    private final ConectaDb db;

    public DaoTerminalImpl() {
        this.db = new ConectaDb();
    }

    @Override
    public List<Object[]> TerminalQRY(String filtro) {
        List<Object[]> list = null;
        String sql = "SELECT "
                + "TER.IDTERMINAL CODIGO,"
                + "TER.DESCRIPCIONTERMINAL DESCRIPCION,"
                + "TER.IDTIPO TIPO,"
                + "TER.IPTERMINAL IP,"
                + "TER.MACADDTERMINAL MAC,"
                + "TER.MARCATERMINAL MARCA,"
                + "TER.MODELOTERMINAL MODELO,"
                + "CASE WHEN TER.ESTADOTERMINAL = 'R' THEN 'REGISTRADA' WHEN TER.ESTADOTERMINAL = 'A' THEN 'ASIGNADA' ELSE 'INACTIVA' END ESTADO"
                + " FROM BDTAS.TERMINAL TER "
                + " WHERE " + filtro
                + " ORDER BY 1";
        Connection cn = db.getBdtas();
        if (cn != null) {
            try {
                PreparedStatement ps = cn.prepareStatement(sql);
                ResultSet rs = ps.executeQuery();
                list = new LinkedList<Object[]>();
                while (rs.next()) {
                    Object[] f = new Object[8];
                    f[0] = rs.getObject(1);
                    f[1] = rs.getObject(2);
                    f[2] = rs.getObject(3);
                    f[3] = rs.getObject(4);
                    f[4] = rs.getObject(5);
                    f[5] = rs.getObject(6);
                    f[6] = rs.getObject(7);
                    f[7] = rs.getObject(8);
                    list.add(f);
                }
            } catch (SQLException e) {
            } finally {
                try {
                    cn.close();
                } catch (SQLException e) {
                }
            }
        }
        return list;
    }

    @Override
    public String TerminalINS(Terminal terminal) {
        String result = null;
        String sql = "INSERT INTO BDTAS.TERMINAL("
                + "IDTERMINAL,"
                + "DESCRIPCIONTERMINAL,"
                + "IDTIPO,"
                + "IPTERMINAL,"
                + "MACADDTERMINAL,"
                + "MARCATERMINAL,"
                + "MODELOTERMINAL,"
                + "ESTADOTERMINAL"
                + ") VALUES(?,?,?,?,?,?,?,?)";

        Connection cn = db.getBdtas();
        if (cn != null) {
            try {
                PreparedStatement ps = cn.prepareStatement(sql);
                ps.setInt(1, terminal.getIDTERMINAL());
                ps.setString(2, terminal.getDESCRIPCIONTERMINAL());
                ps.setInt(3, terminal.getIDTIPO());
                ps.setString(4, terminal.getIPTERMINAL());
                ps.setString(5, terminal.getMACADDTERMINAL());
                ps.setString(6, terminal.getMARCATERMINAL());
                ps.setString(7, terminal.getMODELOTERMINAL());
                ps.setString(8, terminal.getESTADOTERMINAL()); 
                
                int ctos = ps.executeUpdate();
                if (ctos == 0) {
                    result = "0 filas afectadas";
                }

            } catch (SQLException e) {
                result = e.getMessage();
            } finally {
                try {
                    cn.close();
                } catch (SQLException e) {
                    result = e.getMessage();
                }
            }
        }

        return result;
    }

    @Override
    public String TerminalUPD(Terminal terminal) {
        String result = null;
        String sql = "UPDATE BDTAS.TERMINAL SET "
                + "DESCRIPCIONTERMINAL=?,"
                + "IDTIPO=?,"
                + "IPTERMINAL=?,"
                + "MACADDTERMINAL=?,"
                + "MARCATERMINAL=?,"
                + "MODELOTERMINAL=? "
                + "WHERE IDTERMINAL=?";

        Connection cn = db.getBdtas();
        if (cn != null) {
            try {
                PreparedStatement ps = cn.prepareStatement(sql);
                ps.setString(1, terminal.getDESCRIPCIONTERMINAL());
                ps.setInt(2, terminal.getIDTIPO());
                ps.setString(3, terminal.getIPTERMINAL());
                ps.setString(4, terminal.getMACADDTERMINAL());
                ps.setString(5, terminal.getMARCATERMINAL());
                ps.setString(6, terminal.getMODELOTERMINAL());
                ps.setInt(7, terminal.getIDTERMINAL());
                
              
                int ctos = ps.executeUpdate();
                if (ctos == 0) {
                    result = "0 filas afectadas";
                }

            } catch (SQLException e) {
                result = e.getMessage();
            } finally {
                try {
                    cn.close();
                } catch (SQLException e) {
                    result = e.getMessage();
                }
            }
        }

        return result;
    }
    
    @Override
    public Terminal TerminalGET(Integer idterminal) {
        Terminal terminal = null;

        String sql = "SELECT  "
                + "IDTERMINAL,"
                + "DESCRIPCIONTERMINAL,"
                + "IDTIPO,"
                + "IPTERMINAL,"
                + "MACADDTERMINAL,"
                + "MARCATERMINAL,"
                + "MODELOTERMINAL,"
                + "ESTADOTERMINAL "
                + "FROM BDTAS.TERMINAL WHERE IDTERMINAL=?";

        Connection cn = db.getBdtas();
        if (cn != null) {
            try {
                PreparedStatement ps = cn.prepareStatement(sql);
                ps.setInt(1, idterminal);

                ResultSet rs = ps.executeQuery();
                if (rs.next()) {
                    terminal = new Terminal();

                    terminal.setIDTERMINAL(rs.getInt(1));
                    terminal.setDESCRIPCIONTERMINAL(rs.getString(2));
                    terminal.setIDTIPO(rs.getInt(3));
                    terminal.setIPTERMINAL(rs.getString(4));
                    terminal.setMACADDTERMINAL(rs.getString(5));
                    terminal.setMARCATERMINAL(rs.getString(6));
                    terminal.setMODELOTERMINAL(rs.getString(7));
                    terminal.setESTADOTERMINAL(rs.getString(8));
                    
                }
            } catch (SQLException e) {
            } finally {
                try {
                    cn.close();
                } catch (SQLException e) {
                }
            }
        }

        return terminal;
    }

    @Override
    public String TerminalDEL(Terminal terminal) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    

}
